//
//  LoginVC.swift
//  SouWeiLaiSwift
//
//  Created by yxl on 2022/4/12.
//

import UIKit
import Foundation
import QMUIKit
import SnapKit
import SwiftMessages
import IQKeyboardManagerSwift
import RxSwift

class LoginVC: BaseViewController {
    let diposebag = DisposeBag()
    let viewModel = LoginViewModel()
    override func viewDidLoad() {
        super.viewDidLoad()
        self.viewModel.delegate = self
        initUI()
        initAction()
        initLayout()
    }
    
    //MARK: - UI
    func initUI(){
        self.view.addSubview(titleLab)
        self.view.addSubview(phoneNameLab)
        self.view.addSubview(passwordNameLab)
        self.view.addSubview(phoneView)
        self.view.addSubview(passwordView)
        self.view.addSubview(forgotPasswordBtn)
        self.view.addSubview(loginBtn)
        self.phoneView.addSubview(phoneText)
        self.passwordView.addSubview(passwordText)
        self.passwordView.addSubview(seeBtn)
        // 18665309905 18664389864
        phoneText.text = "18664389864"
        passwordText.text = "123456"
    }
    
    // MARK: - 交互
    func initAction(){
        // 账号
        let phoneValid = phoneText.rx.text.orEmpty
            .map{$0.count == 11}
            .share(replay: 1)
        // 密码
        let passwdValid = passwordText.rx.text.orEmpty
            .map{$0.count >= 6}
            .share(replay: 1)
        // 所有输入是否有效
        let everythingValid = Observable.combineLatest(
            phoneValid,
            passwdValid
        ) { $0 && $1 } // 取用户名和密码同时有效
            .share(replay: 1)
        // 登录按钮状态绑定
        everythingValid.bind(to: loginBtn.rx.isEnabled).disposed(by: diposebag)
        everythingValid.bind{[weak self](isEditing) in
            if isEditing{
                self?.loginBtn.gradientButton(with: CGSize(width: kScreenWidth - 48, height: 48), colorArray: [HexColor("30b0d3")!,HexColor("23b96e")!], percentageArray: [0.18,1], gradientType:GradientType.fromLeftBottomToRightTop)
            }else{
                self?.loginBtn.gradientButton(with: CGSize(width: kScreenWidth - 48, height: 48), colorArray: [HexColor("E0E0E0")!,HexColor("E0E0E0")!], percentageArray: [0.18,1], gradientType:GradientType.fromLeftBottomToRightTop)
            }
        }.disposed(by: diposebag)
        // 密文按钮
        seeBtn.rx.tap.subscribe(onNext: {[weak self] in
            self?.passwordText.isSecureTextEntry = (self?.seeBtn.isSelected)!;
            self?.seeBtn.isSelected = !(self?.seeBtn.isSelected)!
        }).disposed(by: diposebag)
        // 登录
        loginBtn.rx.tap.subscribe(onNext:{[weak self] in
            self?.viewModel.requestLogin(pwd: self?.passwordText.text ?? "", mobile: self?.phoneText.text ?? "")
        }).disposed(by: diposebag)
    }
    
    //MARK: - Layout
    func initLayout(){
        titleLab.snp.makeConstraints { make in
            make.left.equalTo(self.view).offset(24)
            make.top.equalTo(self.view).offset(24 + kNavHeight)
        }
        // 账号
        phoneNameLab.snp.makeConstraints { make in
            make.left.equalTo(titleLab)
            make.top.equalTo(titleLab.snp_bottom).offset(40)
        }
        phoneView.snp.makeConstraints { make in
            make.left.equalTo(view).offset(24)
            make.right.equalTo(view).offset(-24)
            make.top.equalTo(phoneNameLab.snp_bottom).offset(8)
            make.height.equalTo(48)
        }
        phoneText.snp.makeConstraints { make in
            make.left.equalTo(phoneView).offset(24);
            make.right.equalTo(phoneView).offset(-24);
            make.top.bottom.equalTo(phoneView);
        }
        // 密码
        passwordNameLab.snp.makeConstraints { make in
            make.left.equalTo(titleLab);
            make.top.equalTo(phoneView.snp_bottom).offset(20);
        }
        passwordView.snp.makeConstraints { make in
            make.left.right.height.equalTo(phoneView);
            make.top.equalTo(passwordNameLab.snp_bottom).offset(8);
        }
        passwordText.snp.makeConstraints { make in
            make.left.equalTo(passwordView).offset(24);
            make.right.equalTo(seeBtn.snp_left).offset(-8);
            make.top.bottom.equalTo(passwordView);
        }
        // 按钮
        loginBtn.snp.makeConstraints { make in
            make.left.right.height.equalTo(phoneView);
            make.top.equalTo(passwordView.snp_bottom).offset(65);
        }
        seeBtn.snp.makeConstraints { make in
            make.right.equalTo(passwordView).offset(-24);
            make.centerY.equalTo(passwordView);
        }
        forgotPasswordBtn.snp.makeConstraints { make in
            make.right.equalTo(passwordView);
            make.top.equalTo(passwordView.snp_bottom).offset(8);
        }
    }
    
    override func viewDidLayoutSubviews() {
        super.viewDidLayoutSubviews()
        phoneView.addCorner(conrners: .allCorners, radius: 24)
        passwordView.addCorner(conrners: .allCorners, radius: 24)
        loginBtn.addCorner(conrners: .allCorners, radius: 24)
    }
    
    // MARK: - 懒加载
    // 标题
    lazy var titleLab : QMUILabel = {
        let titleLab = QMUILabel()
        titleLab.font = BoldFont(24)
        titleLab.textColor = BaseLabelColor
        titleLab.text = LanguageSet("手机密码登录")
        return titleLab
    }()
    // 账号label
    lazy var phoneNameLab : QMUILabel = {
        let phoneNameLab = QMUILabel()
        phoneNameLab.font = Font(14)
        phoneNameLab.textColor = BaseTitleLabelColor
        phoneNameLab.text = LanguageSet("手机号")
        return phoneNameLab
    }()
    // 密码label
    lazy var passwordNameLab : QMUILabel = {
        let passwordNameLab = QMUILabel()
        passwordNameLab.font = Font(14)
        passwordNameLab.textColor = BaseTitleLabelColor
        passwordNameLab.text = LanguageSet("密码")
        return passwordNameLab
    }()
    // 账号输入框背景
    lazy var phoneView : BaseView = {
        let phoneView = BaseView()
        phoneView.backgroundColor = HexColor("F3F7F8")
        return phoneView
    }()
    // 账号输入框
    lazy var phoneText : QMUITextField = {
        let phoneText = QMUITextField()
        phoneText.placeholder = LanguageSet("请输入手机号码")
        phoneText.textColor = BaseTitleLabelColor
        phoneText.placeholderColor = BasePlaceholderColor
        phoneText.font = Font(14)
        phoneText.keyboardType = .phonePad
        phoneText.clearButtonMode = .whileEditing
        phoneText.maximumTextLength = 11
        return phoneText
    }()
    // 密码输入框背景
    lazy var passwordView : BaseView = {
        let passwordView = BaseView()
        passwordView.backgroundColor = HexColor("F3F7F8")
        return passwordView
    }()
    // 密码输入框
    lazy var passwordText : QMUITextField = {
        let passwordText = QMUITextField()
        passwordText.placeholder = LanguageSet("请输入登录密码")
        passwordText.textColor = BaseTitleLabelColor
        passwordText.placeholderColor = BasePlaceholderColor
        passwordText.font = Font(14)
        passwordText.isSecureTextEntry = true;
        return passwordText
    }()
    // 密文切换
    lazy var seeBtn : QMUIButton = {
        let seeBtn = QMUIButton()
        seeBtn.setImage(Image("login_btn_eyeclose_password"), for: .normal)
        seeBtn.setImage(Image("login_btn_eye_password"), for: .selected)
        return seeBtn
    }()
    // 登录
    lazy var loginBtn : QMUIButton = {
        let loginBtn = QMUIButton()
        loginBtn.setTitle(LanguageSet("登录"), for: .normal)
        loginBtn.setTitleColor(BaseWhiteColor, for: .normal)
        loginBtn.backgroundColor = HexColor("E0E0E0")!
        loginBtn.titleLabel?.font = MediumFont(16)
        return loginBtn
    }()
    // 忘记密码
    lazy var forgotPasswordBtn : QMUIButton = {
        let forgotPasswordBtn = QMUIButton()
        forgotPasswordBtn.setTitle(LanguageSet("忘记密码？"), for: .normal)
        forgotPasswordBtn.setTitleColor(HexColor("909090")!, for: .normal)
        forgotPasswordBtn.titleLabel?.font = Font(12)
        return forgotPasswordBtn
    }()
}

extension LoginVC : LoginRequestDelegate{
    // 登录成功回调
    func loginFinishSuccess(loginModel: LoginModel) {
        XLAppDelegate.switchRootToTabbarVC()
        Log(loginModel)
    }
}
